//
//  OPMainTravelNotesController.m
//  OnePiece
//
//  Created by wsy on 15/10/24.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPMainTravelNotesController.h"
#import "OPNoteMainListCell.h"
#import "OPNoteDetailViewController.h"

@interface OPMainTravelNotesController ()
@property (nonatomic, strong) NSMutableArray *datasourceArray;
@end

@implementation OPMainTravelNotesController

- (NSMutableArray *)datasourceArray
{
    if (_datasourceArray == nil) {
        self.datasourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _datasourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureData];
    [self.tableView registerClass:[OPNoteMainListCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
}

// 网络请求数据
- (void)configureData
{
    [OPRequesterManager requestWithType:GET urlString:@"http://app6.117go.com/demo27/php/interestAction.php?start=&submit=getPlaza6_2&length=12&v=i6.4.2&vc=AppStore&vd=ba172e49ddb4ea7a&lang=zh-Hans&verify=816df343dfa571b9632add87f7cc60ae" parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        for (NSDictionary *d in dic[@"obj"][@"list"]) {
            if ([d[@"type"] integerValue] == 4) {
                OPNoteMainListModel *m = [[OPNoteMainListModel alloc]init];
                NSDictionary *dd = d[@"list"][0];
                [m setValuesForKeysWithDictionary:dd];
                [self.datasourceArray addObject:m];
            }
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 700 * SCREEN_WIDTH / 950 + 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio
{
    return self.datasourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OPNoteMainListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    OPNoteMainListModel *m = self.datasourceArray[indexPath.row];
    [cell configureDataWithModel:m];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OPNoteMainListModel *m = self.datasourceArray[indexPath.row];
    OPNoteDetailViewController *detailVC = [[OPNoteDetailViewController alloc]initWithStyle:UITableViewStyleGrouped];
    detailVC.noteId = [NSString stringWithFormat:@"%ld",m.noteId];
    [self.navigationController pushViewController:detailVC animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
